package www.dashu.com.controller;

import www.dashu.com.api.CommonResult;
import www.dashu.com.service.RedisService;
import www.dashu.com.domain.CProductAttributeValue;
import www.dashu.com.dto.CProductAttributeValueQuery;
import www.dashu.com.service.CProductAttributeValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Field;
import java.util.Date;

@Controller
@Api(tags = "CProductAttributeValueController", description = "存储产品参数信息的表")
@RequestMapping("/productAttributeValue")
public class CProductAttributeValueController
{

    @Autowired
    CProductAttributeValueService productAttributeValueService;
    @Autowired
    RedisService redisService;

    @ApiOperation(value = "存储产品参数信息的表列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<CProductAttributeValue>> list(CProductAttributeValueQuery query) {

        try {
            Page<CProductAttributeValue> pageData = productAttributeValueService.findAll(query);
            return CommonResult.success(pageData);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @ApiOperation(value = "新增存储产品参数信息的表")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody CProductAttributeValue productAttributeValue) {
        productAttributeValue.setId(productAttributeValueService.createId(productAttributeValue));

        Field[] fields = productAttributeValue.getClass().getDeclaredFields();
        for(Field field : fields){
            field.setAccessible(true);
            if(field.getName().toLowerCase().contains("time")){
                try {
                    field.set(productAttributeValue, new Date());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        productAttributeValueService.save(productAttributeValue);
        return CommonResult.success();
    }

    @ApiOperation(value = "修改存储产品参数信息的表")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody CProductAttributeValue productAttributeValue) {

        Field[] fields = productAttributeValue.getClass().getDeclaredFields();
        for(Field field : fields){
            field.setAccessible(true);
            if(field.getName().toLowerCase().contains("updatetime")){
                try {
                    field.set(productAttributeValue, new Date());
                 } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        productAttributeValueService.save(productAttributeValue);
        return CommonResult.success();
    }

    @ApiOperation(value = "删除存储产品参数信息的表")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult delete(@RequestBody CProductAttributeValue productAttributeValue) {
        productAttributeValueService.deleteById(productAttributeValue.getId());
        return CommonResult.success();
    }

}
